﻿using System;
using System.IO;
using System.Xml;
using Moonlite.Platform.General;
using Moonlite.ExtensionApi.GlobalInterfaces;

namespace Moonlite.Compiling
{
    public static class CompilerReader
    {
        private const string Path = "Compilers\\Compilers.xml";

        public static CompilerCollection Load()
        {
            bool fileExists = File.Exists(Path);
            Stream xmlStream = File.Open(Path, FileMode.OpenOrCreate);
            if (fileExists == false)
            {
                WriteDefaultXmlText();
            }

            XmlDocument document = new XmlDocument();
            document.Load(xmlStream);
            CompilerCollection compilers = new CompilerCollection();
            foreach (XmlNode node in document.ChildNodes)
            {
                if (node.Name.ToLower() == "compilers")
                {
                    foreach (XmlNode compilerNode in node.ChildNodes)
                    {
                        if (compilerNode.Name.ToLower() == "compiler")
                        {
                            DefaultCompiler compiler = new DefaultCompiler();
                            foreach (XmlAttribute attribute in compilerNode.Attributes)
                            {
                                switch (attribute.Name.ToLower())
                                {
                                    case "name":
                                        compiler.Name = attribute.Value;
                                        break;
                                    case "exepath":
                                        compiler.ExePath = attribute.Value;
                                        break;
                                    case "defaultcliargs":
                                        compiler.DefaultCLIArgs = attribute.Value;
                                        break;
                                    case "addpathtomap":
                                        compiler.AddPathToMap = bool.Parse(attribute.Value);
                                        break;
                                    case "type":
                                        try
                                        {
                                            compiler.Type = (CompilerType)Enum.Parse(typeof(CompilerType), attribute.Value, true);
                                        }
                                        catch (Exception exception)
                                        {
                                            Logger.Log += exception.Message + Environment.NewLine;
                                        }

                                        break;
                                    default:
                                        break;
                                }
                            }

                            compilers.Add(compiler);
                        }
                    }
                }
            }

            return compilers;
        }

        public static void Save(CompilerCollection compilers)
        {
            XmlDocument document = new XmlDocument();
            XmlNode compilersNode = document.CreateNode(XmlNodeType.Element, "Compilers", null);
            foreach (ICompiler compiler in compilers)
            {
                XmlNode compilerNode = document.CreateNode(XmlNodeType.Element, "Compiler", null);
                XmlAttribute nameAttribute = document.CreateAttribute("Name");
                XmlAttribute exePathAttribute = document.CreateAttribute("ExePath");
                XmlAttribute defaultCLIArgsAttribute = document.CreateAttribute("DefaultCLIArgs");
                XmlAttribute addPathToMapAttribute = document.CreateAttribute("AddPathToMap");
                nameAttribute.Value = compiler.Name;
                exePathAttribute.Value = compiler.ExePath;
                defaultCLIArgsAttribute.Value = compiler.DefaultCLIArgs;
                addPathToMapAttribute.Value = compiler.AddPathToMap.ToString();
                compilerNode.Attributes.Append(nameAttribute);
                compilerNode.Attributes.Append(exePathAttribute);
                compilerNode.Attributes.Append(defaultCLIArgsAttribute);
                compilerNode.Attributes.Append(addPathToMapAttribute);
                compilersNode.AppendChild(compilerNode);
            }

            document.AppendChild(compilersNode);
            Stream xmlStream = File.Open(Path, FileMode.OpenOrCreate);
            document.Save(xmlStream);
        }

        internal static void WriteDefaultXmlText()
        {
            CompilerCollection compilers = new CompilerCollection();
            compilers.Add(new DefaultCompiler("JassHelper", "jasshelper\\jasshelper.exe", "common.j Blizzard.j", true));
            Save(compilers);
        }
    }
}
